/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.itintegration.catalogue.mek.ejb;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import lombok.extern.slf4j.Slf4j;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author khaleddaham
 */
@Slf4j
@Startup
@Singleton
public class MekEjb {

    private XPath xpath = null;
    private Document doc = null;
    private Map<String, String> hsaToKombikaMap = new HashMap<>();

    /**
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public MekEjb() throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.parse(getClass().getResourceAsStream("/META-INF/mek.xml"));
        XPathFactory xPathfactory = XPathFactory.newInstance();
        xpath = xPathfactory.newXPath();
    }

    @PostConstruct
    private void init() {
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        try {
            log.info("MekEjb init");
            SAXParser saxParser = saxParserFactory.newSAXParser();
            MekHandler handler = new MekHandler();
            saxParser.parse(getClass().getResourceAsStream("/META-INF/mek.xml"), handler);
            hsaToKombikaMap = handler.getMap();
        } catch (ParserConfigurationException | SAXException | IOException e) {
            log.error("Unable to parse mek.xml", e);
        }
    }

    /**
     *
     * @param hsaID
     * @return
     */
    public String getKombika(String hsaID) {
        //return hsaToKombikaMap.get(hsaID);
        return "";
    }

    /**
     *
     * @param kombika
     * @return
     * @throws XPathExpressionException
     */
    public String getHsaID(String kombika) throws XPathExpressionException {
        String expression = "//mappning/Kombikakod[text()=\"" + kombika + "\"]/following-sibling::HSAId/text()";
        XPathExpression expr = xpath.compile(expression);
        NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        for (int i = 0; i < nl.getLength(); i++) {
            Node childNode = nl.item(i);
            return childNode.getTextContent();
        }
        return "";
    }

    /**
     *
     * @return @throws XPathExpressionException
     */
    public NodeList listKombika() throws XPathExpressionException {
        String expression = "//mappning/";
        XPathExpression expr = xpath.compile(expression);
        return (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    }
}